// pages/newPageBankCentent/newPageBankCentent.js
var app = getApp();
var com = require('../../common.js');
Page({
  data: {
    startTime: '2010-01-01',
    endTime: '2020-01-01',
    info: {
      idcode_other: '',
      idcode_positive: '',
    }
  },
  onLoad: function (options) {
    var _this = this
    _this.onInfoPage()
  },
  onReady: function () { },
  onShow: function () { },
  onHide: function () { },
  onUnload: function () { },
  onPullDownRefresh: function () { },
  onReachBottom: function () { },
  onShareAppMessage: function () { },
  onInfoPage() {
    console.log(this.data)
    var _this = this,
      info = _this.data.info
    com.link(app.link.sub, {}, function (res) {
      console.log(res)
      info.idcode_other = res.data.response.sub.idcode_other ? res.data.response.sub.idcode_other : ''
      info.idcode_other_url = res.data.response.sub.idcode_other_url ? res.data.response.sub.idcode_other_url : ''
      info.idcode_positive = res.data.response.sub.idcode_positive ? res.data.response.sub.idcode_positive : ''
      info.idcode_positive_url = res.data.response.sub.idcode_positive_url ? res.data.response.sub.idcode_positive_url : ''
      if (res.data.response.sub.idcode_other) {
        _this.setData({
          startTime: res.data.response.sub.valid_from,
          endTime: res.data.response.sub.valid_until
        })
      }
      _this.setData({
        info: info
      })
    }, 'GET')
  },
  onUpdateImage(e) {
    var id = e.currentTarget.id,
      _this = this,
      info = _this.data.info
    wx.chooseImage({
      count: 1,
      success: function (res) {
        console.log(res)
        if (id == 1) {
          info.idcode_positive_url = res.tempFilePaths[0]
          _this.setData({
            info: info
          })
        } else if (id == 2) {
          info.idcode_other_url = res.tempFilePaths[0]
          _this.setData({
            info: info
          })
        }
        console.log(_this.data)
        wx.uploadFile({
          url: app.link.temporary,
          filePath: res.tempFilePaths[0],
          name: 'file',
          formData: {
            file: res.tempFilePaths[0]
          },
          success: function (res) {
            console.log(res)
            let img = JSON.parse(res.data)
            console.log(img)
            if (id == 1) {
              info.idcode_positive = img.response.file.name
              _this.setData({
                info: info
              })
            } else {
              info.idcode_other = img.response.file.name
              _this.setData({
                info: info
              })
            }
          }
        })
      }
    })
  },
  bindTimeChange(e) {
    console.log(e)
    var id = e.currentTarget.id,
      _this = this
    if (id == 1) {
      _this.setData({
        startTime: e.detail.value
      })
    } else {
      _this.setData({
        endTime: e.detail.value
      })
    }
  },
  onJumpPage() {
    console.log(this.data)
    var _this = this,
      info = _this.data
    com.link(app.link.onIdCardUpdata, {
      idcode_positive: info.info.idcode_positive,
      idcode_other: info.info.idcode_other,
      valid_from: info.startTime,
      valid_until: info.endTime
    }, function (res) {
      console.log(res)
      if (res.data.state) {
        wx.navigateTo({
          url: '/pages/newPageBankCrrent/newPageBankCrrent',
        })
      } else {
        wx.showToast({
          title: res.data.message,
          icon: 'none'
        })
      }

    })

  }
})